package database

import (
	log2 "chess/log"
	"chess/tools"
	"context"
	"errors"
	"fmt"
	"github.com/go-redis/redis/v8"
	"strconv"
)

func GetGameRecord(ctx context.Context, roomId int) ([]string, error) {
	redisPath := tools.GetEnv("REDIS_PATH", "localhost:6379")
	redisPD := tools.GetEnv("REDIS_PASSWORD", "")
	rdb2 := redis.NewClient(&redis.Options{
		Addr:     redisPath,
		Password: redisPD, // no password set
		DB:       5,       // use default DB
		//PoolSize: 500, // 连接池大小
	})

	//_, err = rdb2.SAdd(ctx, strconv.Itoa(roomId), message).Result()
	result, err := rdb2.LRange(ctx, strconv.Itoa(roomId), 0, -1).Result()
	if err != nil {
		writeLog, err3 := log2.WriteLog(context.Background(), "chessgame", "GetGameRecord", strconv.Itoa(roomId), "Warring", "Game record does not exist")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err3 != nil {
			fmt.Println("写入日志失败")
		}
		return nil, err
	}
	if len(result) == 0 {
		writeLog, err3 := log2.WriteLog(context.Background(), "chessgame", "GetGameRecord", strconv.Itoa(roomId), "Warring", "no combat record")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err3 != nil {
			fmt.Println("写入日志失败")
		}
		return nil, errors.New("no combat record")
	}
	return result, err
}
